/**
 * 
 */
package com.alstudio.utils.formater.string;

import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;

import android.content.Context;

/**
 * @author alonso lee
 * 
 */
public class StringUtils {

	private static final String SIMPLE_PWD = "123456";

	public static boolean isContainBlockStr(Context context, int blockStrArray,
			String str) {

		String[] blockingKeyWord = context.getResources().getStringArray(
				blockStrArray);
		for (String s : blockingKeyWord) {
			if (str.contains(s)) {
				// 含有不合法的关键字
				String msg = String.format(
						context.getString(R.string.TxtBlockingKeywordAlert), s);
				MyToastUtil.getInstance().showToastOnCenter(msg);
				return true;
			}
		}
		return false;
	}

	/**
	 * 检测昵称中是否含有违禁字符
	 * 
	 * @param context
	 * @param nick
	 * @return true标示含有违禁字符
	 */
	public static boolean isNickContainBlockStr(Context context, String nick) {
		return isContainBlockStr(context, R.array.keyword_filtering, nick);
	}

	/**
	 * 获取str中是否含有违禁字符
	 * 
	 * @param context
	 * @param blockStrArray
	 * @param str
	 * @return 没有违禁字符返回Null
	 */
	public static String getBlockStr(Context context, int blockStrArray,
			String str) {

		String[] blockingKeyWord = context.getResources().getStringArray(
				blockStrArray);
		for (String s : blockingKeyWord) {
			if (str.contains(s)) {
				// 含有不合法的关键字
				String msg = String.format(
						context.getString(R.string.TxtBlockingKeywordAlert), s);
				MyToastUtil.getInstance().showToastOnCenter(msg);
				return s;
			}
		}
		return null;
	}

	/**
	 * 检测昵称中是否含有违禁字符，有則直接返回違禁字符，否則為Null
	 * 
	 * @param context
	 * @param nick
	 * @return
	 */
	public static String getNickBlockStr(Context context, String nick) {
		return getBlockStr(context, R.array.keyword_filtering, nick);
	}

	/**
	 * 检测是否为简单密码
	 * 
	 * @param pwd
	 * @return
	 */
	public static boolean isSimplePassword(String pwd) {
		if (SIMPLE_PWD.equals(pwd))
			return true;
		for (int i = 0; i < pwd.length(); i++) {
			if (i != (pwd.length() - 1)) {
				if (pwd.charAt(i) != pwd.charAt(i + 1))
					return false;
			}
		}
		return true;
	}

	public static int getStringArrayIndex(String[] str, String value) {

		int index = 0;

		if (null != str && null != value) {
			for (int i = 0; i < str.length; i++) {

				if (value.equals(str[i]) || value == str[i]) {

					index = i;
				}
			}
		}
		return index;
	}

}
